﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VBOX
{
    class Msh
    {
        public static VboxSerialPort MshSerial = null;
        public static bool flag_capslock = false;
        public static bool flag_shift = false;
        public static byte[] key_tmp = new byte[10];

        public static void MshKeyDown(object sender, KeyEventArgs e)
        {
            if (MshSerial == null)
                return;
            
            if (e.KeyCode == Keys.CapsLock)
            {
                if (flag_capslock)
                    flag_capslock = false;
                else
                    flag_capslock = true;

                return;
            }

            if (e.KeyValue == 16)
            {
                flag_shift = true;
                return;
            }
            // tbxRecvData.AppendText(e.KeyValue.ToString());


            if (flag_capslock)  //大写字母
            {
                if ((e.KeyValue >= 65) && (e.KeyValue <= 90))
                    key_tmp[0] = (byte)(e.KeyValue);
            }
            else    //小写字母
            {
                if (flag_shift)
                {
                    if ((e.KeyValue >= 65) && (e.KeyValue <= 90))
                        key_tmp[0] = (byte)(e.KeyValue);
                }
                else
                {
                    if ((e.KeyValue >= 65) && (e.KeyValue <= 90))
                        key_tmp[0] = (byte)(e.KeyValue + 32); //大写转小写字母
                }
            }

            if ((e.KeyValue >= 48) && (e.KeyValue <= 57))   //主键盘数字
            {
                if (flag_shift)
                {
                    switch (e.KeyValue)
                    {
                        case 48:
                            key_tmp[0] = (byte)(41);  //)
                            break;
                        case 49:
                            key_tmp[0] = (byte)(33);  //!
                            break;
                        case 50:
                            key_tmp[0] = (byte)(64);  //@
                            break;
                        case 51:
                            key_tmp[0] = (byte)(35);  //#
                            break;
                        case 52:
                            key_tmp[0] = (byte)(36);  //$
                            break;
                        case 53:
                            key_tmp[0] = (byte)(37);  //%
                            break;
                        case 54:
                            key_tmp[0] = (byte)(94);  //^
                            break;
                        case 55:
                            key_tmp[0] = (byte)(38);  //&
                            break;
                        case 56:
                            key_tmp[0] = (byte)(42);  //*
                            break;
                        case 57:
                            key_tmp[0] = (byte)(40);  //(
                            break;
                    }
                }
                else
                {
                    key_tmp[0] = (byte)(e.KeyValue);
                }
            }
            else if ((e.KeyValue >= 96) && (e.KeyValue <= 105)) //小键盘数字
            {
                key_tmp[0] = (byte)(e.KeyValue - 48);
            }
            else if (e.KeyValue == 187)  //+=
            {
                if (flag_shift)
                    key_tmp[0] = (byte)(61);
                else
                    key_tmp[0] = (byte)(43);
            }
            else if (e.KeyValue == 188)  //<,
            {
                if (flag_shift)
                    key_tmp[0] = (byte)(60);
                else
                    key_tmp[0] = (byte)(44);
            }
            else if (e.KeyValue == 189)  //-_
            {
                if (flag_shift)
                    key_tmp[0] = (byte)(95);
                else
                    key_tmp[0] = (byte)(45);
            }
            else if (e.KeyValue == 190)  //>.
            {
                if (flag_shift)
                    key_tmp[0] = (byte)(62);
                else
                    key_tmp[0] = (byte)(46);
            }
            else if (e.KeyValue == 191)  //?/
            {
                if (flag_shift)
                    key_tmp[0] = (byte)(63);
                else
                    key_tmp[0] = (byte)(47);
            }
            else if (e.KeyValue == 186)  //:;
            {
                if (flag_shift)
                    key_tmp[0] = (byte)(58);
                else
                    key_tmp[0] = (byte)(59);
            }
            else if (e.KeyValue == 219)  //{[
            {
                if (flag_shift)
                    key_tmp[0] = (byte)(123);
                else
                    key_tmp[0] = (byte)(91);
            }
            else if (e.KeyValue == 220)  //|\
            {
                if (flag_shift)
                    key_tmp[0] = (byte)(124);
                else
                    key_tmp[0] = (byte)(92);
            }
            else if (e.KeyValue == 221)  //}]
            {
                if (flag_shift)
                    key_tmp[0] = (byte)(125);
                else
                    key_tmp[0] = (byte)(93);
            }
            else if (e.KeyValue == 222)  //"'
            {
                if (flag_shift)
                    key_tmp[0] = (byte)(34);
                else
                    key_tmp[0] = (byte)(39);
            }
            else if (e.KeyValue == 13)  //回车键
            {
                // key_tmp[0] = (byte)13;    //\r
                // sp.Write(key_tmp, 0, 1);
                key_tmp[0] = (byte)10;    //\n
            }
            else if (e.KeyValue == 9)  //tab
            {
                key_tmp[0] = (byte)9;
            }
            else if (e.KeyValue == 8)  //backspace
            {
                key_tmp[0] = (byte)8;
            }
            else if (e.KeyValue == 32)  //空格键
            {
                key_tmp[0] = (byte)32;
            }
            else if (e.KeyValue == 38)  //PgUp
            {
                key_tmp[0] = (byte)0x1b;

                MshSerial.Write(key_tmp, 0, 1);
                key_tmp[0] = (byte)0x5b;
                MshSerial.Write(key_tmp, 0, 1);
                key_tmp[0] = (byte)0x41;
            }
            else if (e.KeyValue == 40)  //PgDn
            {
                key_tmp[0] = (byte)0x1b;
                MshSerial.Write(key_tmp, 0, 1);
                key_tmp[0] = (byte)0x5b;
                MshSerial.Write(key_tmp, 0, 1);
                key_tmp[0] = (byte)0x42;
            }
            else if (e.KeyValue == 37)  //Home
            {
                // key_tmp[0] = (byte)0x1b;
                // sp.Write(key_tmp, 0, 1);
                // key_tmp[0] = (byte)0x5b;
                // sp.Write(key_tmp, 0, 1);
                // key_tmp[0] = (byte)0x44;
                return;
            }
            else if (e.KeyValue == 39)  //End
            {
                // key_tmp[0] = (byte)0x1b;
                // sp.Write(key_tmp, 0, 1);
                // key_tmp[0] = (byte)0x5b;
                // sp.Write(key_tmp, 0, 1);
                // key_tmp[0] = (byte)0x43;
                return;
            }

            if (MshSerial.IsOpen() == true)
                MshSerial.Write(key_tmp, 0, 1);
            
        }
    }
}
